package com.sunmouse.toolkit.widget.canvas;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toolkit.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/9/14 0014.
 */
public class SnakeView extends View {
    private Paint mPaint;
    private int tileSize;
    private Bitmap[] materials;
    private int[][] indexValue;//decide which bitmap will be drawn at this point;

    private int xOffset;
    private int yOffset;

    private int xCount;//horizontal capacity
    private int yCount;//vertical capacity

    private ArrayList<Coordinate> snakeCoors;
    private ArrayList<Coordinate> appleCoors;


    public SnakeView(Context context) {
        this(context, null);
    }

    public SnakeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProperty();
    }

    /**
     * init properties  here
     */
    private void initProperty() {
        tileSize = 24;
        Resources r = this.getResources();
        //load materials
        this.materials = new Bitmap[4];
        loadMaterial(Type.WALL, r.getDrawable(R.mipmap.ic_launcher));
        loadMaterial(Type.SNAKE, r.getDrawable(R.mipmap.red_tile));
        loadMaterial(Type.APPLE, r.getDrawable(R.mipmap.green_tile));

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

    }

    /**
     * load Material
     */
    private void loadMaterial(int type, Drawable drawable) {
        Bitmap mBitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        drawable.setBounds(0, 0, tileSize, tileSize);
        drawable.draw(canvas);
        materials[type] = mBitmap;

    }

    /**
     * redraw all tile
     */
    private void drawAll() {
        drawWall();
        drawSnake();
        drawApple();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        LogUtils.d("-------w="+getWidth()+"-h="+getHeight());
        resetTable();
    }

    /**
     * reset table
     */
    private void resetTable() {
        int w = getWidth();
        int h = getHeight();

        xCount = w / tileSize;
        yCount = h / tileSize;

        xOffset = w - xCount * tileSize;
        yOffset = h - yCount * tileSize;

        indexValue = new int[yCount][xCount];

        drawAll();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < yCount; i++) {
            for (int j = 0; j < xCount; j++) {
                if (indexValue[i][j] > 0)
                    canvas.drawBitmap(materials[indexValue[i][j]], xOffset + j * tileSize, yOffset + i * tileSize, mPaint);
            }
        }
    }

    /**
     * draw wall
     */
    private void drawWall() {
        //set horizontal bitmap
        for (int i = 0; i < xCount; i++) {
            indexValue[0][i] = Type.WALL;
            indexValue[yCount - 1][i] = Type.WALL;
        }
        //set vertex bitmap
        for (int i = 0; i < yCount; i++) {
            indexValue[i][0] = Type.WALL;
            indexValue[i][xCount - 1] = Type.WALL;
        }
    }

    /**
     * draw random apple
     */
    private void drawApple() {

    }

    /**
     * draw snake :treat as queue {FIFO}
     */
    private void drawSnake() {

    }


    /**
     * material type
     */
    static class Type {
        public static final int WALL = 1;
        public static final int SNAKE = 2;
        public static final int APPLE = 3;
    }

    /**
     * a coordinate class
     */
    class Coordinate {
        private int x;
        private int y;

        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        @Override
        public String toString() {
            return "Coordinate{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    }

}
